.TH std::ranges::get(std::ranges::subrange) 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::ranges::get(std::ranges::subrange) \- std::ranges::get(std::ranges::subrange)

.SH Synopsis
   Defined in header <ranges>
   template< std::size_t N, class I, class S, ranges::subrange_kind K
   >
                                                                      \fB(1)\fP \fI(since C++20)\fP
       requires ((N == 0 && std::copyable<I>) || N == 1)

   constexpr auto get( const ranges::subrange<I, S, K>& r );
   template< std::size_t N, class I, class S, ranges::subrange_kind K
   >
                                                                      \fB(2)\fP \fI(since C++20)\fP
       requires (N < 2)

   constexpr auto get( ranges::subrange<I, S, K>&& r );
   namespace std { using ranges::get; }                               \fB(3)\fP \fI(since C++20)\fP

   1) Obtains the iterator or sentinel from a subrange lvalue (or a const rvalue) when
   N == 0 or N == 1, respectively. It is mainly provided for structured binding
   support.
   2) Same as \fB(1)\fP, except that it takes a non-const subrange rvalue.
   3) (1,2) are imported into namespace std, which simplifies their usage and makes
   every subrange with a copyable iterator a pair-like type.

.SH Parameters

   r - a subrange

.SH Return value

   1) An iterator or sentinel copy constructed from the stored one when N == 0 or N ==
   1, respectively.
   2) Same as \fB(1)\fP, except that the iterator is move constructed if N == 0 and I does
   not satisfy copyable.

.SH Possible implementation

   template<std::size_t N, class I, class S, std::ranges::subrange_kind K>
       requires ((N == 0 && std::copyable<I>) || N == 1)
   constexpr auto get(const std::ranges::subrange<I, S, K>& r)
   {
       if constexpr (N == 0)
           return r.begin();
       else
           return r.end();
   }

   template<std::size_t N, class I, class S, std::ranges::subrange_kind K>
       requires (N < 2)
   constexpr auto get(std::ranges::subrange<I, S, K>&& r)
   {
       if constexpr (N == 0)
           return r.begin(); // may perform move construction
       else
           return r.end();
   }

.SH Example


// Run this code

 #include <array>
 #include <iostream>
 #include <iterator>
 #include <ranges>

 int main()
 {
     std::array a{1, -2, 3, -4};

     std::ranges::subrange sub_a{std::next(a.begin()), std::prev(a.end())};
     std::cout << *std::ranges::get<0>(sub_a) << ' ' // == *(begin(a) + 1)
               << *get<1>(sub_a) << '\\n';            // == *(end(a) - 1)

     *get<0>(sub_a) = 42; // OK
 //  *get<2>(sub_a) = 13; // hard error: index can only be 0 or 1
 }

.SH Output:

 -2 -4

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR    Applied to           Behavior as published              Correct behavior
                       the overload for const lvalue was         it is removed from the
   LWG 3589 C++20      ill-formed if N == 0                      overload set
                       and I does not model copyable

.SH See also

   Structured binding \fI(C++17)\fP binds the specified names to sub-objects or tuple
                              elements of the initializer
   get(std::tuple)            tuple accesses specified element
   \fI(C++11)\fP                    \fI(function template)\fP
   get(std::pair)             accesses an element of a pair
   \fI(C++11)\fP                    \fI(function template)\fP
   get(std::array)            accesses an element of an array
   \fI(C++11)\fP                    \fI(function template)\fP
